//
//  DMCCycleImages.m
//  06- CycleImages
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "DMCCycleImages.h"
@interface DMCCycleImages ()
@property (nonatomic, strong) UIScrollView *cycleScrollView;
@property (nonatomic, strong) UIPageControl *imagesPageControl;
@end
@implementation DMCCycleImages
#pragma mark - scrollView 的 Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 改变pageControl的对应下标
    NSInteger page = _cycleScrollView.contentOffset.x / self.bounds.size.width;
    _imagesPageControl.currentPage = page - 1;
    if (page == 0) {
        // 改变滚动视图的偏移量
        // _imagesArray是首尾添加后的数组
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imagesArray.count - 2), 0);
        _imagesPageControl.currentPage = _imagesArray.count - 1;
    } else if (page == _imagesArray.count - 1) {
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _imagesPageControl.currentPage = 0;
    }
}
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 子控件初始化时 ,frame不要与初始化方法的frame一样
        [self createCycleScrollView];
        [self createImagesPage:frame];
    }
    return self;
}
//#warning 后期需要添加page的位置改变, 这里是默认位置
- (void)createImagesPage:(CGRect)frame {
    _imagesPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    _imagesPageControl.center = CGPointMake(frame.size.width / 2, frame.size.height - _imagesPageControl.frame.size.height / 2);
    [_imagesPageControl addTarget:self action:@selector(imagePageChange:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_imagesPageControl];

}
- (void)imagePageChange:(UIPageControl *)page {
    [_cycleScrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
}
- (void)createCycleScrollView {
    _cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _cycleScrollView.backgroundColor = [UIColor cyanColor];
    _cycleScrollView.pagingEnabled = YES;
    _cycleScrollView.showsHorizontalScrollIndicator = NO;
    _cycleScrollView.delegate = self;
    [self addSubview:_cycleScrollView];
}
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    // 实现page的点跟随照片的滑动而变化
//    _imagesPageControl.currentPage = scrollView.contentOffset.x / self.bounds.size.width - 1;
//    
//}

#pragma mark - 自定义的方法
- (void)setImagesWithArray:(NSArray *)array {
    // 根据传过来的数组, 进行处理, 生成图片数组
    NSArray *imageArray = [self handleArray:array];
    // 逻辑判断时, 需要用到数组内容
    self.imagesArray = imageArray;
    // 根据数组生成对应的ImageView
    [self createAllImageViewsWithImageArray:imageArray];
    // 根据图片数组, 设置对应的视图
    _imagesPageControl.numberOfPages = array.count;
    _cycleScrollView.contentSize = CGSizeMake(imageArray.count * self.bounds.size.width, 0);
    // 因为在第一个位置上添加了最后一张图, 要让轮播图看起来在第一张, 所以通过控制偏移量, 来实现
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
}
#pragma mark - 判断类型(不知道别人使用你的类的时候传进来的类型是什么)
- (NSArray *)handleArray:(NSArray *)array {
    id firstObject = [array firstObject];
    // 最终的图片数组
    NSMutableArray *imageArray = [NSMutableArray array];
    // 判断对象是不是某种类型, isKindOfClass
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 把字符串数组转换成图片数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imageArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        // 将数组变成可变的数组
        [imageArray setArray:array];
    }
    // 获取到第一张和最后一张图片
    UIImage *firstImage = [imageArray firstObject];
    UIImage *lastImage = [imageArray lastObject];
    // 将两张图片分别加到第一张和最后一张
    [imageArray insertObject:lastImage atIndex:0];
    [imageArray addObject:firstImage];
    // 将可变数组变成不可变的并返回
    return [imageArray copy];
}
// 将可变数组变成不可变数组
// 例将可变数组 imageArray 变成不可变数组 [imageArray copy]
#pragma mark - 直接使用数组创建ImageView
- (void)createAllImageViewsWithImageArray:(NSArray *)imageArray {
    for (int i = 0; i < imageArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imageArray[i];
        [_cycleScrollView addSubview:imageView];
    }
 
}

@end
